package com.employee.seckill.controller;

import com.employee.seckill.entity.Employee;
import com.employee.seckill.service.EmployeeService;
import com.employee.seckill.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * ClassName: EmployeeController
 * Package: com.employee.seckill.controller
 * Description:员工管理模块-接口层
 *
 * @Author 刘荟雅
 * @Create 2025/9/10 19:29
 * @Version 1.0
 */
@RestController
@RequestMapping("/employee")
@Tag(name = "员工接口")
public class EmployeeController {

    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

//    @GetMapping
//    @Operation(summary = "查询全部")
//    public List<Employee> listAll() {
//        return employeeService.listAll();
//    }

    @GetMapping
    @Operation(summary = "1 分页查询全部员工")
    //统一格式后前端永远收到：JSON
    public Result<PageInfo<Employee>> listAll(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeService.listAll();
        return Result.success(new PageInfo<>(list));
    }
//    public PageInfo<Employee> listAll(
//            @RequestParam(defaultValue = "1") int pageNum,
//            @RequestParam(defaultValue = "10") int pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<Employee> list = employeeService.listAll();
//        return new PageInfo<>(list);
//    }

    @GetMapping("/search")
    @Operation(summary = "2 条件分页查询员工（姓名模糊+部门精确）")
//    public PageInfo<Employee> search(
//            @RequestParam(required = false) String name,
//            @RequestParam(required = false) String dept,
//            @RequestParam(defaultValue = "1") int pageNum,
//            @RequestParam(defaultValue = "10") int pageSize) {
//        return employeeService.listByCondition(name, dept, pageNum, pageSize);
//    }
    public Result<PageInfo<Employee>> search(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String dept,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Employee> page = employeeService.listByCondition(name, dept, pageNum, pageSize);
        return Result.success(page);
    }




    @GetMapping("/{id}")
    @Operation(summary = "3 根据ID查询员工")
//    public Employee findById(@PathVariable Long id) {
//        return employeeService.findById(id);
//    }
    //验证异常处理
    public Result<Employee> findById(@PathVariable Long id) {
        Employee emp = employeeService.findById(id); // 这里会抛异常
        return Result.success(emp);                  // 统一包装
    }

    @PostMapping
    @Operation(summary = "4 新增员工")
    public Result<Void> add(@RequestBody Employee emp) {
        employeeService.add(emp);
        return Result.success(null);
    }

    @PutMapping
    @Operation(summary = "5 修改员工")
    public Result<Void> update(@RequestBody Employee emp) {
        employeeService.update(emp);
        return Result.success(null);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "6 删除员工")
    public Result<Void> delete(@PathVariable Long id) {
        employeeService.delete(id);
        return Result.success(null);
    }
}
